package test.src.awt_swing;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JProgressBarDemo {
    JFrame jFrame = new JFrame("这里测试进度条");

    JProgressBar jProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);

    JCheckBox indeterminate = new JCheckBox("不确定进度");
    JCheckBox noBorder = new JCheckBox("不绘制边框");

    private void init() {
        //组装视图
        Box vBox = Box.createVerticalBox();
        vBox.add(indeterminate);
        vBox.add(noBorder);

        Box content = Box.createHorizontalBox();

        content.add(vBox);
        content.add(jProgressBar);

        jFrame.add(content);

        jProgressBar.setMinimum(0);
        jProgressBar.setMaximum(100);

        jProgressBar.setBorderPainted(true);
        jProgressBar.setStringPainted(true);
        jProgressBar.setIndeterminate(false);

        indeterminate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean selected = indeterminate.isSelected();
                jProgressBar.setIndeterminate(selected);
                jProgressBar.setStringPainted(!selected);
            }
        });

        noBorder.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean selected = noBorder.isSelected();
                jProgressBar.setBorderPainted(!selected);
            }
        });

        jFrame.pack();
        jFrame.setVisible(true);

        for (int i = 0; i <= 100; i++) {
            jProgressBar.setValue(i);

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        new JProgressBarDemo().init();
    }
}
